#!/bin/env/python3
import logging
from logging.handlers import RotatingFileHandler

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from redis import StrictRedis

from config import config_dict,Config


db = SQLAlchemy()
rd = None
def log_file(LEVEL_NAME):
    # 设置记录等级
    logging.basicConfig(level=LEVEL_NAME)
    # 日志记录器
    file_log_handler = RotatingFileHandler('logs/log', maxBytes=1024 * 1024,backupCount=20)
    # 格式
    file_log_formatter = logging.Formatter('%(levelname)s %(filename)s:%(lineno)d %(message)s')
    file_log_handler.setFormatter(file_log_formatter)
    logging.getLogger().addHandler(file_log_handler)


def create_app(config_name):
    app = Flask(__name__)
    config = config_dict.get(config_name)
    log_file(config.LEVEL_NAME)
    db.init_app(app)
    global rd
    rd = StrictRedis(host=Config.REDIS_HOST,port=Config.REDIS_PORT,decode_responses=True)


